package com.leancloud.circle.manage;

import com.leancloud.circle.bean.CollectionBean;
import com.leancloud.circle.bean.FansBean;

import java.util.ArrayList;
import java.util.List;

import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.user.CurrentUser;

public class CircleGlobal {

    public final static int category_Gold = 145; //黄金
    public final static int category_Exchange = 144; //外汇
    public final static int category_HongKongStocks = 142; //港股
    public final static int category_USStocks = 141; //美股
    public final static int category_Stocks = 140; //股票
    public final static int category_Futures = 139; //期货

    public final static int FUND_TOPIC = 160; //区块链日记
//    public final static int category_Virtual = 177; //币圈
    public final static int category_Virtual = 165; //以太坊行情话题
    public final static int CurrentType = category_Virtual;

    public static List<FansBean> fans = new ArrayList<>();
    public static List<FansBean> concerneds = new ArrayList<>();
    public static List<CollectionBean> collections = new ArrayList<>();

    public static CircleInterface getCircleImp() {
        return CircleImp.getInstance();
    }

    public static void updateAll() {
        updateCollection();
        updateConcerned();
        updateFans();
    }

    public static void updateFans() {
        CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
        if (user == null || !user.isLogin())
            return;
        getCircleImp().requestFans(new CircleBack<List<FansBean>>() {
            @Override
            public void onBack(List<FansBean> beans, String err) {
                if (err == null) {
                    fans.clear();
                    fans.addAll(beans);
                }
            }
        });
    }

    public static void updateConcerned() {
        CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
        if (user == null || !user.isLogin())
            return;
        getCircleImp().requestConcerned(new CircleBack<List<FansBean>>() {
            @Override
            public void onBack(List<FansBean> beans, String err) {
                if (err == null) {
                    concerneds.clear();
                    concerneds.addAll(beans);
                }
            }
        });
    }

    public static void updateCollection() {
        CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
        if (user == null || !user.isLogin())
            return;
        getCircleImp().requestCollection(user.getToken(), 1, "50", null, new CircleBack<List<CollectionBean>>() {
            @Override
            public void onBack(List<CollectionBean> beans, String err) {
                if (err == null) {
                    collections.clear();
                    collections.addAll(beans);
                }
            }
        });
    }

    public static void clean() {
        fans.clear();
        concerneds.clear();
        collections.clear();
    }

    public static boolean isConcerned(int userId) {
        return getConcerned(userId) != null;
    }

    public static FansBean getConcerned(int userId) {
        if (concerneds.isEmpty())
            return null;
        for (FansBean bean : concerneds) {
            if (userId == bean.getId()) {
                return bean;
            }
        }
        return null;
    }

    public static boolean isFan(int userId) {
        return getFan(userId) != null;
    }

    public static FansBean getFan(int userId) {
        if (fans.isEmpty())
            return null;
        for (FansBean bean : fans) {
            if (userId == bean.getId()) {
                return bean;
            }
        }
        return null;
    }

    public static boolean isCollections(int collectionId) {
        return getCollection(collectionId) != null;
    }

    public static CollectionBean getCollection(int collectionId) {
        if (collections.isEmpty())
            return null;
        for (CollectionBean bean : collections) {
            if (bean.getObject_id() == collectionId)
                return bean;
        }
        return null;
    }

    public static List<FansBean> getFans() {
        return fans;
    }

    public static List<FansBean> getConcerneds() {
        return concerneds;
    }

    public static List<CollectionBean> getCollections() {
        return collections;
    }
}
